package com.liangxy.news.ui.main.profile;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.liangxy.news.R;
import com.liangxy.news.model.News;
import com.liangxy.news.ui.detail.NewsDetailActivity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Adapter for history list
 */
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.HistoryViewHolder> {

    private List<News> historyList;
    
    /**
     * Constructor
     * @param historyList List of news history
     */
    public HistoryAdapter(List<News> historyList) {
        this.historyList = historyList;
    }
    
    @NonNull
    @Override
    public HistoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news, parent, false);
        return new HistoryViewHolder(view);
    }
    
    @Override
    public void onBindViewHolder(@NonNull HistoryViewHolder holder, int position) {
        News news = historyList.get(position);
        holder.bind(news);
    }
    
    @Override
    public int getItemCount() {
        return historyList == null ? 0 : historyList.size();
    }
    
    /**
     * Update data
     * @param newData New data to be displayed
     */
    public void updateData(List<News> newData) {
        this.historyList = newData;
        notifyDataSetChanged();
    }
    
    /**
     * ViewHolder for history item
     */
    static class HistoryViewHolder extends RecyclerView.ViewHolder {
        private TextView tvTitle;
        private TextView tvSource;
        private TextView tvDate;
        private ImageView ivThumbnail;
        
        public HistoryViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tvTitle);
            tvSource = itemView.findViewById(R.id.tvSource);
            tvDate = itemView.findViewById(R.id.tvDate);
            ivThumbnail = itemView.findViewById(R.id.ivThumbnail);
        }
        
        /**
         * Bind news data to view
         * @param news News data
         */
        public void bind(News news) {
            if (news != null) {
                // 显示标题
                tvTitle.setText(news.getTitle());
                
                // 显示来源
                tvSource.setText(news.getSource());
                
                // 显示日期
                String dateStr = news.getDate();
                tvDate.setText(dateStr);
                
                // 显示缩略图
                String thumbnail = news.getThumbnail();
                if (thumbnail != null && !thumbnail.isEmpty()) {
                    ivThumbnail.setVisibility(View.VISIBLE);
                    Glide.with(itemView.getContext())
                            .load(thumbnail)
                            .placeholder(R.drawable.placeholder_image)
                            .error(R.drawable.placeholder_image)
                            .into(ivThumbnail);
                } else {
                    ivThumbnail.setVisibility(View.GONE);
                }
                
                // 设置点击事件
                itemView.setOnClickListener(v -> {
                    Intent intent = new Intent(itemView.getContext(), NewsDetailActivity.class);
                    intent.putExtra(NewsDetailActivity.EXTRA_NEWS_ID, news.getUniquekey());
                    itemView.getContext().startActivity(intent);
                });
                
                // 显示浏览时间
                try {
                    long lastReadTime = news.getLastReadTime();
                    if (lastReadTime > 0) {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
                        String readTimeStr = sdf.format(new Date(lastReadTime));
                        tvDate.setText(String.format("阅读于: %s", readTimeStr));
                    }
                } catch (Exception e) {
                    // 忽略日期格式化错误
                }
            }
        }
    }
} 